import { login, logout, getInfo } from '@/api/login'
import { getToken, setToken, removeToken, setUserId } from '@/utils/tool'
import { Message } from 'element-ui';
const user = {
  namespaced: true,
  state: {
    token: getToken(),
    userInfo: {},
    avatar: require('@/assets/images/avatar.png'),
    roles: [],
    permissions: [],
    refreshMainKey: true,
  },

  mutations: {
    // SET_TOKEN: (state, token) => {
    //   state.token = token
    // },
    // SET_USER_ID: (state, id) => {
    //   state.id = id
    // },
    SET_USERINFO: (state, info) => {
      state.userInfo = info
      localStorage.setItem('userInfo', JSON.stringify(info))
    },
    SET_AVATAR: (state, avatar) => {
      state.avatar = avatar
    },
    SET_ROLES: (state, roles) => {
      state.roles = roles
    },
    SET_PERMISSIONS: (state, permissions) => {
      state.permissions = permissions
    },
    SET_REFRESH_MAIN: (state, data) => {
      //让key改变,来刷新app-mian
      state.refreshMainKey = data;
    }
  },

  actions: {
    // 登录
    Login({ commit }, userInfo) {
      const username = userInfo.username.trim()
      const password = userInfo.password
      return new Promise((resolve, reject) => {
        login({ username, password }).then(res => {
          if (res.code == 200) {
            setToken(res.data.token)
            setUserId(res.data.user_id)
            commit('SET_USERINFO', res.data);
            // commit('SET_TOKEN', res.data.token)
            // commit('SET_USER_ID', res.data.user_id)
            resolve({
              code: 200,
              msg: res.msg
            })
          } else {
            Message.error(res.msg)
          }
        }).catch(error => {
          reject(error)
        })
      })
    },

    // 获取用户信息
    GetInfo({ commit, state }) {
      return new Promise((resolve, reject) => {
        getInfo().then(res => {
          const user = res.user
          const avatar = (user.avatar == "" || user.avatar == null) ? require("@/assets/images/profile.jpg") : process.env.VUE_APP_BASE_API + user.avatar;
          if (res.roles && res.roles.length > 0) { // 验证返回的roles是否是一个非空数组
            commit('SET_ROLES', res.roles)
            commit('SET_PERMISSIONS', res.permissions)
          } else {
            commit('SET_ROLES', ['ROLE_DEFAULT'])
          }
          commit('SET_ID', user.userId)
          commit('SET_NAME', user.userName)
          commit('SET_AVATAR', avatar)
          resolve(res)
        }).catch(error => {
          reject(error)
        })
      })
    },

    // 退出系统
    LogOut({ commit, state }) {
      return new Promise((resolve, reject) => {
        // commit('SET_TOKEN', '')
        localStorage.clear();
        resolve()
      })
    },

    // 前端 登出
    FedLogOut({ commit }) {
      return new Promise(resolve => {
        commit('SET_TOKEN', '')
        removeToken()
        resolve()
      })
    },
    //刷新Main区域
    refreshMain({ commit }, data) {
      return new Promise((resolve) => {
        commit("SET_REFRESH_MAIN", data)
        resolve();
      })
    }
  }
}

export default user;

